package com.dengqi.mymodule.activity.network;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.dengqi.mymodule.R;
import com.dengqi.mymodule.activity.network.activity.AddMemberActivity;
import com.dengqi.mymodule.activity.network.activity.CardActivity;
import com.dengqi.mymodule.activity.network.activity.BaseInfoActivity;
import com.dengqi.mymodule.activity.network.activity.MemberListActivity;
import com.dengqi.mymodule.activity.network.bean.AccountBean;
import com.dengqi.mymodule.activity.network.activity.LoginActivity;
import com.dengqi.mymodule.activity.network.module.AccountContract;
import com.dengqi.mymodule.activity.network.util.ConstantUtil;
import com.module.retrofit.bean.ResponseBean;
import com.module.my.RxNet;
import com.module.retrofit.RetrofitHelper;
import com.module.retrofit.callback.BaseObserver;
import com.module.retrofit.utils.HeaderUtils;
import com.module.retrofit.utils.RxSchedulers;
import com.module.retrofit.utils.ToastUtil;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.LinkedHashMap;
import java.util.Map;


public class NetWorkActivity extends RxAppCompatActivity  {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);

        //放application里面
        Map<String, String> headers = new LinkedHashMap<>();
        headers.put(ConstantUtil.HEADER_CHANNEL, "3");
        headers.put(ConstantUtil.HEADER_DEVICE, "2");
        headers.put(ConstantUtil.HEADER_VERSION, "1.0");
        headers.put(ConstantUtil.HEADER_CID, "0");
        headers.put(ConstantUtil.HEADER_LAT, "0");
        headers.put(ConstantUtil.HEADER_LNG, "0");
        HeaderUtils.saveHeaders(headers);
    }

    public void onBtn1(View view) {
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("channel", "2");
        parmas.put("device", "2");
        parmas.put("version", "1.0.0");
        parmas.put("phone", "13916379354");
        parmas.put("loginType", "1");
        parmas.put("captcha", "123456");
        RetrofitHelper.getApiService(AccountContract.Model.class)
                .login(parmas)
                .compose(RxSchedulers.applySchedulers())
                //靠对话框取消订阅 或者靠rxlife生命周期取消订阅
                .compose(this.bindUntilEvent(ActivityEvent.STOP))
                .subscribe(new BaseObserver<ResponseBean<AccountBean>>(this) {
                    @Override
                    public void onSuccess(ResponseBean<AccountBean> result) {
                        ToastUtil.showToast(result.getData().getNick());
                        HeaderUtils.setHeader(ConstantUtil.HEADER_TOKEN, result.getData().getToken());
                    }

                });
    }


    public void onBtn2(View view) {
        //好处：可以不用继承RxAppCompatActivity
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("channel", "2");
        parmas.put("device", "2");
        parmas.put("version", "1.0.0");
        parmas.put("phone", "13916379354");
        parmas.put("loginType", "1");
        parmas.put("captcha", "123456");
        RxNet.with(this)
                .setObservable(RetrofitHelper.getApiService(AccountContract.Model.class).login(parmas))
                .subscribe(new BaseObserver<ResponseBean<AccountBean>>(this) {
                    @Override
                    public void onSuccess(ResponseBean<AccountBean> result) {
                        ToastUtil.showToast(result.getData().getNick());
                        HeaderUtils.setHeader(ConstantUtil.HEADER_TOKEN, result.getData().getToken());
                    }
                });
    }

    public void onBtn3(View view) {
        startActivity(new Intent(this, LoginActivity.class));

    }

    public void onBtn4(View view) {
        startActivity(new Intent(this, MemberListActivity.class));
    }

    public void onBtn5(View view) {
        startActivity(new Intent(this, AddMemberActivity.class));
    }

    public void onBtn6(View view) {
        startActivity(new Intent(this, CardActivity.class));
    }

    public void onBtn7(View view) {
        startActivity(new Intent(this, BaseInfoActivity.class));
    }
}
